package com.leyou.item.comsume;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailSerivce;
import com.leyou.item.service.SpuService;
import com.leyou.pojo.dto.ParamDTO;
import com.leyou.pojo.dto.SkuDTO;
import com.leyou.pojo.dto.SpuDTO;
import com.leyou.pojo.dto.SpuDetailDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodComsume {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailSerivce spuDetailSerivce;


    /**
     * 分页查询spu
     * @param brandId  品牌id
     * @param categoryId 商品分类id
     * @param id spu id
     * @param page 当前页
     * @param rows 每页大小
     * @param saleable 是否上架
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> findSpuPage(
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ) {
        return ResponseEntity.ok(spuService.findSpuPage(brandId,categoryId,id,page,rows,saleable));
    }

    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("spu")
    public ResponseEntity<Void> addGood(@RequestBody SpuDTO spuDTO){
        spuService.addGood(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品
     * @param spuDTO
     * @return
     */
    @PutMapping("spu")
    public ResponseEntity<Void> UpdateGood(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id批量查询sku
     * @param ids SkuId
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> findSkusByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(skuService.findSkusByIds(ids));
    }

    /**
     * 根据spuID查询sku的集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> findSkusBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(skuService.findSkusBySpuId(id));
    }

    /**
     * 根据id查询商品详情
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> findDetailById(@RequestParam("id") Long id) {
        return ResponseEntity.ok(spuDetailSerivce.findDetailById(id));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id 商品id
     * @param searching 是否参与搜索
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<ParamDTO>> findParamBySpuId(@RequestParam("id") Long id, @RequestParam(value = "searching", required = false) Boolean searching) {
        return ResponseEntity.ok(spuService.findParamBySpuId(id,searching));
    }

    /**
     * 修改商品上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleableById(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {
        spuService.updateSaleableById(id,saleable);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryInfoBySpuId(@PathVariable("id") Long id) {
        return ResponseEntity.ok(spuService.queryInfoBySpuId(id));
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(spuService.querySpuById(id));
    }

    /**
     * 批量减库存
     * @param map
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> lessenRepertory(@RequestBody Map<Long, Integer> map) {
        skuService.lessenRepertory(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
